package com.example.utils.util;

/**
 * @Description
 * @ClassName IPToLong
 * @Author xiaojiang
 * @Date 2022/6/21 下午 12:48
 */
public class IPToLong {

    /**
     * @return String
     * @Description 把long类型的Ip转为一般Ip类型：xx.xx.xx.xx
     * @Param : ip
     * @Author 江
     * @Date 2022/3/28 下午 16:35
     */
    public static String getIpFromLong(Long ip) {
        String s1 = String.valueOf((ip & 4278190080L) / 16777216L);
        String s2 = String.valueOf((ip & 16711680L) / 65536L);
        String s3 = String.valueOf((ip & 65280L) / 256L);
        String s4 = String.valueOf(ip & 255L);
        return s1 + "." + s2 + "." + s3 + "." + s4;
    }

    /**
     * @return Long
     * @Description 把xx.xx.xx.xx类型的转为long类型的
     * @Param : ip
     * @Author 江
     * @Date 2022/3/28 下午 16:35
     */
    public static Long getIpFromString(String ip) {
        Long ipLong = 0L;
        String ipTemp = ip;
        ipLong = ipLong * 256
                + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256
                + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256
                + Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
        ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
        ipLong = ipLong * 256 + Long.parseLong(ipTemp);
        return ipLong;
    }


}
